$(function(){
    //SetFormValue();
    //获取url参数
	function GetQueryString(name){
		var reg = new RegExp("(^|&)"+name+"=([^&]*)(&|$)");
		var r = window.location.search.substr(1).match(reg);
		if(r!=null)return unescape(r[2]); return null;
    }

    function SetFormValue(){

        //var select=GetQueryString("select");
        var property=GetQueryString("property");

        /*if(select !=null && select.toString().length>=1){
			$("#select_select").val(select);
        }*/
        if(property !=null && property.toString().length>=1){
			$("#property_select").val(property);
        }
    }
    //移除宿舍
    dormitory_remove=function(table){
        dormitoryList=new Array();
        instructor_id=$("#uid").val();
        grade=$("#grade").val();
        major=$("#major").val();
        apartment_id=$(table).parents("tr").find(".apartment_id").val();
        dormitory_id=$(table).parents("tr").find(".dormitory_id").val();
        floor=$(table).parents("tr").find(".floor").html();
        beds=$(table).parents("tr").find(".beds_select").val();
        instrutor_already_used_quantity=$(table).parents("tr").find(".instrutor_already_used_quantity").html();
        if(instrutor_already_used_quantity==0){
            layer.msg('宿舍未分配，不需要移除', {
				time: 2000, //2s后自动关闭
			});
        }else{
            dormitory=new Object();
            dormitory.instructorID=instructor_id;
            dormitory.grade=grade;
            dormitory.major=major;
            dormitory.apartmentID=apartment_id;
            dormitory.floor=floor;
            dormitory.dormitoryID=dormitory_id;
            dormitoryList[0]=dormitory;

            dormitoryJSON=JSON.stringify(dormitoryList);
            layer.confirm('确定移除',{btn:['确定','取消']},
            function(){
                $.post("./php/instructor-dormitory-allocating.php",
                {action:2,data:dormitoryJSON},
                function(result){
                    
                    object=JSON.parse(result);
                    switch(object.code){
                        case '2001':
                            layer.msg(object.message,{time:2000,area:['120px']},
                            function(){
                                location.replace(location.href);
                            });
                            break;
                        case '2004':
                            layer.msg(object.message);
                            break;
                        default:layer.msg(object.message);break;
                    }
                });
            },
            function(){});
        }

        
    }

    //移除选中的宿舍
    dormitory_remove_checkbox=function(){
        dormitoryList=new Array();
        $(":checkbox[name='dormitory_allocating']:checked").each(function(index,e){
            instructor_id=$("#uid").val();
            grade=$("#grade").val();
            major=$("#major").val();
            apartment_id=$(this).parents("tr").find(".apartment_id").val();
            dormitory_id=$(this).val();
            floor=$(this).parents("tr").find(".floor").html();
            beds=$(this).parents("tr").find(".beds_select").val();
            instrutor_already_used_quantity=$(this).parents("tr").find(".instrutor_already_used_quantity").html();
            if(instrutor_already_used_quantity==0){
                $(this).prop("checked",false);
            }else{
                dormitory=new Object();
                dormitory.instructorID=instructor_id;
                dormitory.grade=grade;
                dormitory.major=major;
                dormitory.apartmentID=apartment_id;
                dormitory.floor=floor;
                dormitory.dormitoryID=dormitory_id;
                dormitoryList[index]=dormitory;
            }
            
        });

        if(dormitoryList.length>0){
            dormitoryJSON=JSON.stringify(dormitoryList);
            $.post("./php/instructor-dormitory-allocating.php",
            {action:2,data:dormitoryJSON},
            function(result){
                object=JSON.parse(result);
                switch(object.code){
                    case '2001':
                        //$.Huimodalalert(object.message,2000);
                        layer.msg(object.message,{btn:['确定']},
                        function(){
                            location.replace(location.href);
                        });
                        break;
                    case '2004':
                        layer.msg(object.message);
                        break;
                    default:break;
                }
            });
        }else{
            layer.msg('未选中任何项');
        }

        
    }

    //分配选中宿舍
    allocating_dormitory_checkbox=function(){
        dormitoryList=new Array();
        $(":checkbox[name='dormitory_allocating']:checked").each(function(index,e){
            instructor_id=$("#uid").val();
            grade=$("#grade").val();
            major=$("#major").val();
            apartment_id=$(this).parents("tr").find(".apartment_id").val();
            dormitory_id=$(this).val();
            floor=$(this).parents("tr").find(".floor").html();
            beds=$(this).parents("tr").find(".beds_select").val();

            dormitory=new Object();
            dormitory.instructorID=instructor_id;
            dormitory.grade=grade;
            dormitory.major=major;
            dormitory.apartmentID=apartment_id;
            dormitory.floor=floor;
            dormitory.dormitoryID=dormitory_id;
            dormitory.beds=beds;
            dormitory.action=0;

            dormitoryList[index]=dormitory;
        });
        if(dormitoryList.length>0){
            dormitoryJSON=JSON.stringify(dormitoryList);
            $.post("./php/instructor-dormitory-allocating.php",
            {action:1,data:dormitoryJSON},
            function(result){
                object=JSON.parse(result);
                switch(object.code){
                    case '2001':
                        layer.msg(object.message);
                        break;
                    case '2002':
                        layer.msg(object.message);
                        break;
                    case '2003':
                        layer.msg(object.message);
                        break;
                    case '2004':
                        layer.msg(object.message);
                        break;
                    default:layer.msg(object.message);break;
                }
            });
        }else{
            layer.msg('未选中任何项');
        }
        
    }

    //分配单个宿舍
    allocating_dormitory=function(table,apartment_id){
        instructor_id=$("#uid").val();
        grade=$("#grade").val();
        major=$("#major").val();
        dormitory_id=$(table).parents("tr").find(".dormitory_id").val();
        floor=$(table).parents("tr").find(".floor").html();
        beds=$(table).parents("tr").find(".beds_select").val();
        //available_quantity
        available_quantity=$(table).parents("tr").find(".available_quantity").html();

        dormitory=new Object();
        dormitory.instructorID=instructor_id;
        dormitory.grade=grade;
        dormitory.major=major;
        dormitory.apartmentID=apartment_id;
        dormitory.floor=floor;
        dormitory.dormitoryID=dormitory_id;
        dormitory.beds=beds;
        dormitory.action=0;

        dormitoryList=new Array();
        dormitoryList[0]=dormitory;

        if(available_quantity>0){
            dormitoryJSON=JSON.stringify(dormitoryList);
            //alert(allocatingJSON);
            $.post("./php/instructor-dormitory-allocating.php",
            {action:1,data:dormitoryJSON},
            function(result){
                object=JSON.parse(result);
                switch(object.code){
                    case '2001':
                        //$.Huimodalalert(object.message,2000);
                        layer.msg(object.message,{time:2000});
                        break;
                    case '2004':
                        //$.Huimodalalert(object.message,2000);
                        layer.msg(object.message,{time:2000});
                        break;
                    default:break;
                }
            });
        }else{
            layer.msg("无可分配床位");
        }
        

    }

    //选择整个宿舍
    /*checkbox_all=function(t,name){
        id=$(t).attr("for");
        var checked = $("#"+id).prop("checked");
        $('.'+id).prop("checked",!checked);
        //alert(checked);
        if(!checked){
            $(".label-allocating"+'.c'+name).css("background-color","#80b4e4");
        }else{
            $(".label-allocating"+'.c'+name).css("background-color","#fafafa");
        }
    }

    $(".label-allocating").click(function(){
        id=$(this).attr("for");
        str=id.split("-");
        checkbox_dorm=str[0];
        if(!($("#"+id).prop("checked"))){ 
            $(this).css("background-color","#80b4e4");
        }else{
            $(this).css("background-color","#fafafa");
            $("#"+checkbox_dorm).prop("checked",false);
        }
        
    });*/

    $("select").change(function(){
        id=$(this).attr("id");
        switch(id){
            //查看选择
            case 'select_select':
                var select=$("#select_select").val();
                switch(parseInt(select)){
                    //选择所有
                    case 0:
                        $("#apartment_select").html(apartmentList);
                        object=apartmentFloor[0];
                        floor='';
                        for(i=1;i<=parseInt(object.floor);i++){
                            floor=floor+'<option value="'+i+'">'+i+'</option>';
                        }
                        $("#floor_select").html('<option value="0" selected="">所有</option>'+floor);
                        break;
                    //选中已分配
                    case 1:
                        apartment_all='<option value="0">所有</option>'+apartmentList;
                        $("#apartment_select").html(apartment_all);
                        $("#floor_select").append('<option value="0" selected="">所有</option>');
                        break;
                    default:break;
                }
                break;
            //宿舍分类选择
            case 'property_select':
                if($(this).val()==0){
                    if($("#select_select").val()==1){
                        apartment_all='<option value="0">所有</option>'+apartmentList;
                        $("#floor_select").html('<option value="0" selected="">所有</option>');
                    }else{
                        apartment_all=apartmentList;
                    }
                    
                    $("#apartment_select").html(apartment_all);
                    
                }else{
                    apartment='';
                    for(i=0;i<apartmentFloor.length;i++){
                        object=apartmentFloor[i];
                        if(object.property==$(this).val()){
                        apartment=apartment+'<option value="'+object.id+'">'+object.apartment_name+'</option>';
                        }
                    }
                    if($("#select_select").val()==1){
                        apartment='<option value="0">所有</option>'+apartment;
                    }
                    $("#apartment_select").html(apartment);
                }
                break;
            //公寓选择
            case 'apartment_select':
                if($(this).val()==0){
                    $("#floor_select").html('<option value="0">所有</option>');
                }else{
                    for(i=0;i<apartmentFloor.length;i++){
                        object=apartmentFloor[i];
                        if(object.id==$(this).val()){
                            floor='';
                            for(i=1;i<=parseInt(object.floor);i++){
                                floor=floor+'<option value="'+i+'">'+i+'</option>';
                            }
                            $("#floor_select").html('<option value="0" selected="">所有</option>'+floor);
                            break;
                        }
                    }
                }
                break;
            case 'allocating_page':
                pageTurning($(this).val());
                break;
            default:break;
        }
    });

    function pageTurning(page){
        //uid=1234561
        //&grade=2014&major=1&select=0&property=0&apartmentID=1&floor=1&search=
        var select=GetQueryString("select");
        var uid=GetQueryString("uid");
		var major=GetQueryString("major");
        var grade=GetQueryString("grade");
        var property=GetQueryString("property");
        var apartmentID=GetQueryString("apartmentID");
        var floor=GetQueryString("floor");
        var search=GetQueryString("search");
        
		if(select ==null || select==''){
			select=0;
        }
        if(uid ==null){
			uid='';
        }
        if(major ==null || major==''){
			major=0;
        }
        if(grade ==null || grade==''){
			grade=0;
        }
        if(property ==null || property==''){
			property=0;
        }
        if(apartmentID ==null || apartmentID==''){
			apartmentID=0;
        }
        if(floor ==null || floor==''){
			floor=0;
		}
		if(search ==null){
			search='';
        }
        url="./instructor-dormitory-allocating.php?select="+select+"&uid="+uid+"&major="+major
        +"&grade="+grade+"&property="+property+"&apartmentID="+apartmentID+"&floor="+floor+"&page="+page+"&search="+search;
		window.location.href=url;
    }
    
});